package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblRpaBankCard;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRpaBankCardService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RPA3.0专项费用充值-银行卡信息表 前端控制器
 * @author xiaochuan
 * @since 2021-03-09
 */

@Slf4j
@Api(tags = "RPA3.0专项费用充值-银行卡信息表",description = "RPA3.0专项费用充值-银行卡信息表")
@RestController
@RequestMapping("/tblRpaBankCard")
public class TblRpaBankCardController extends MergeApiExcelController<TblRpaBankCard, Map<String, Object>> {

    @Resource
    private ITblRpaBankCardService tblRpaBankCardService;

    @Override
    public IBaseService init() {
        return tblRpaBankCardService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblRpaBankCard.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "cardNo", "phone");
        super.setParamsModel(model);
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblRpaBankCard foot = tblRpaBankCardService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    public String verifyImportObject(TblRpaBankCard detail, AnalysisContext analysisContext) {
        String message = super.verifyImportObject(detail,analysisContext);
        if(StringUtils.isNotEmpty(message)){
            return message;
        }
        try {
            Map<String,Object> map = new HashMap<>(16);
            map.put("cardNo",detail.getCardNo());
            setParamsModel(map);
            List<TblRpaBankCard> list = tblRpaBankCardService.selectByParam(map);
            if(!CollectionUtils.isEmpty(list)){
                return "卡号"+detail.getCardNo()+"已存在";
            }
            map = new HashMap<>(16);
            map.put("sort",detail.getSort());
            list = tblRpaBankCardService.selectByParam(map);
            if(!CollectionUtils.isEmpty(list)){
                return "优先级"+detail.getSort()+"已存在";
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return "验证时出错";
        }

        return null;
    }

    @Override
    protected void saveData(List<TblRpaBankCard> datas) throws ServiceException {
        List<String> cardList = new ArrayList<>();
        List<String> sortList = new ArrayList<>();
        for(TblRpaBankCard data : datas){
            String cardNo = data.getCardNo();
            if(cardList.contains(cardNo)){
                throw new ServiceException("有两行卡号一样:"+cardNo);
            }else{
                cardList.add(cardNo);
            }

            String sort = data.getSort();
            if(sortList.contains(sort)){
                throw new ServiceException("有两行优先级一样:"+sort);
            }else{
                sortList.add(sort);
            }
        }
        super.saveData(datas);
    }


}

